package goext

import (
	"errors"
	"os"
)

// DiskReader 磁盘读取。
type DiskReader struct {
	path string
	f    *os.File
}

// NewDiskReader 生成一个。
func NewDiskReader(dev string) (*DiskReader, error) {
	f, err := os.Open(dev)
	if err != nil {
		return nil, err
	}

	dr := &DiskReader{
		path: dev,
		f:    f,
	}
	return dr, nil
}

func (dr DiskReader) Read(start int64, count int) ([]byte, int, error) {
	if count <= 0 {
		return nil, 0, errors.New("invalid count")
	}

	data := make([]byte, count, count)

	rCount, err := dr.f.ReadAt(data, start)
	return data, rCount, err
}
